<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <DataTree class="w-1/5 xl:w-1/6" @select="handleSelect" />
    <BasicTable @register="registerTable" class="w-4/5 xl:w-5/6" :searchInfo="searchInfo">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate">录入数据</a-button>
      </template>
    </BasicTable>
    <DataModal @register="registerModal" @success="handleSuccess" />
  </PageWrapper>
</template>
<script lang="ts" setup>
import { reactive, onMounted } from 'vue';

import { BasicTable, useTable } from '@/components/Table';
import { PageWrapper } from '@/components/Page';
import DataTree from './DataTree.vue';

import { useModal } from '@/components/Modal';
import DataModal from './DataModal.vue';

import { columns, searchFormSchema } from './data';
import { getDeviceDataPage } from '@/api/collection/data/index';

defineOptions({ name: 'AccountManagement' });

const [registerModal, { openModal }] = useModal();
const searchInfo = reactive<Recordable>({});
const [registerTable, { reload, updateTableDataRecord, setPagination }] = useTable({
  title: '数据列表',
  api: getDeviceDataPage,
  rowKey: 'id',
  columns,
  immediate: false,
  formConfig: {
    labelWidth: 120,
    schemas: searchFormSchema,
    autoSubmitOnEnter: true,
  },
  useSearchForm: true,
  showTableSetting: false,
  bordered: true,
  handleSearchInfoFn(info) {
    console.log('handleSearchInfoFn', info);
    return info;
  },
});

function handleCreate() {
  openModal(true, {
    isUpdate: false,
  });
}

function handleSuccess({ isUpdate, values }) {
  if (isUpdate) {
    // 演示不刷新表格直接更新内部数据。
    // 注意：updateTableDataRecord要求表格的rowKey属性为string并且存在于每一行的record的keys中
    const result = updateTableDataRecord(values.id, values);
    // console.log(result);
  } else {
    reload();
  }
}

function handleSelect(params: any) {
  // console.log(params);
  searchInfo.nodeId = params.id;
  searchInfo.nodeType = params.type;
  // searchInfo.pageNo = params.pageNo;
  setPagination({ current: 1 })
  reload();
}
</script>
